/**  
 * @Title: GoodsController.java
 * @Package com.silverbox.sweeporder.controller.index
 * @author litao
 * @date 2018年1月17日
 */
package com.silverbox.sweeporder.controller.index;

import java.text.ParseException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.silverbox.core.result.CoreResponse;
import com.silverbox.core.result.CoreResponse.StatusInfo;
import com.silverbox.core.util.StringUtils;
import com.silverbox.core.util.Utils;
import com.silverbox.sweeporder.common.Constants;
import com.silverbox.sweeporder.controller.index.entryvo.KoubeiDishEntryVO;
import com.silverbox.sweeporder.controller.index.entryvo.KoubeiRecomEntryVO;
import com.silverbox.sweeporder.controller.index.entryvo.NeedGoodsAddCart;
import com.silverbox.sweeporder.controller.index.vo.CopyGoodsVO;
import com.silverbox.sweeporder.controller.index.vo.GoodsPractVO;
import com.silverbox.sweeporder.controller.index.vo.GoodsPropsCVO;
import com.silverbox.sweeporder.controller.index.vo.GoodsVO;
import com.silverbox.sweeporder.controller.index.vo.GroupPackageSuitVO;
import com.silverbox.sweeporder.controller.index.vo.KoubeiRecommendGoodsVO;
import com.silverbox.sweeporder.controller.index.vo.OrdinaryPackageVO;
import com.silverbox.sweeporder.controller.index.vo.QrcodeGoodsVO;
import com.silverbox.sweeporder.controller.index.vo.ShopperGoodsVO;
import com.silverbox.sweeporder.service.index.GoodsService;
import com.silverbox.waimai.entity.extend.AlreadyGoods;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * ClassName: GoodsController
 * 
 * @author litao
 * @date 2018年1月17日
 */
@Controller
@RequestMapping("goods")
@Api(tags = { "商品接口" })
public class GoodsController {
	private static Logger logger = LoggerFactory.getLogger(GoodsController.class);

	@Autowired
	private GoodsService goodsService;

	@RequestMapping(value = "getShopperGoodsInfo", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "获取店铺首页商品信息(可调用)")
	public CoreResponse<ShopperGoodsVO> getShopperGoodsInfo(HttpSession session, HttpServletRequest request,
                @RequestParam(required = true) Integer shopperId, @RequestParam(required = true) Integer storeId,
                @RequestParam(required = false) String shopId, @RequestParam(required = false) Long machineId,
                @RequestParam(required = false) Integer clientOrderMode, @RequestParam(required = false) String tableUid,
                @RequestParam(required = false) String appointmentTime,@RequestParam(required = false) String reserveDate) throws ParseException {
		CoreResponse<ShopperGoodsVO> shopperGoods = new CoreResponse<ShopperGoodsVO>();
		if(storeId==null || shopperId==null){
			shopperGoods.setStatusInfo(StatusInfo.ERROR_40002);
			shopperGoods.setSubMsg("参数异常！请检查参数");
			return shopperGoods;
		}
		//判断调用接口来源是否是微信
		String userAgent = request.getHeader("User-Agent").toLowerCase();
		Integer clientType = Utils.getClientType(userAgent);


		shopperGoods = goodsService.getShopperGoodsInfo(shopperId,storeId,shopId,machineId,tableUid,clientOrderMode,clientType,appointmentTime,reserveDate);
		return shopperGoods;
	}	

	@RequestMapping(value = "getShopperPropInfo", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "获取店铺未绑定到菜品的属性信息即店铺级属性（可调用）")
	public CoreResponse<List<GoodsPractVO>> getShopperPropInfo(HttpSession session,
			@RequestParam(required = true) Integer shopperId, @RequestParam(required = true) Integer storeId) {
		CoreResponse<List<GoodsPractVO>> res = new CoreResponse<List<GoodsPractVO>>();
		if(shopperId==null || storeId==null){
			res.setStatusInfo(StatusInfo.ERROR_40002);
			res.setSubMsg("参数异常！请检查参数");
			return res;
		}
		res = goodsService.getShopperPropInfo(shopperId, storeId);
		return res;
	}
	
	@RequestMapping(value = "getSingleGroupPackageGoods", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "获取单个商品的组合套餐信息（可调用）")
	public CoreResponse<List<GroupPackageSuitVO>> getSingleGroupPackageGoods(HttpSession session,
			@RequestParam(required = true) Integer shopperId, @RequestParam(required = true) Integer storeId,
			@RequestParam(required = true) Integer goodsId) {
		CoreResponse<List<GroupPackageSuitVO>> res = new CoreResponse<List<GroupPackageSuitVO>>();
	
		if(shopperId==null || storeId==null || goodsId==null){
			res.setStatusInfo(StatusInfo.ERROR_40002);
			res.setSubMsg("参数异常！请检查参数");
			return res;
		}
		
		List<GroupPackageSuitVO> groupPackages = goodsService.getSingleGroupPackageGoods(storeId, shopperId,
				goodsId);
		
		res.setData(groupPackages);
		return res;
	}
	
	@RequestMapping(value = "getGoodsInfoByGoodsName", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "获取商品信息根据商品名称搜索（可调用）")
	public CoreResponse<List<GoodsVO>> getGoodsInfoByGoodsName(HttpSession session,
			@RequestParam(required = true) String goodsName, @RequestParam(required = true) Integer storeId,
			@RequestParam(required = true) Integer shopperId, @RequestParam(required = false) String shopId,
			@RequestParam(required = false) Long machineId) {
		
		CoreResponse<List<GoodsVO>> res = new CoreResponse<List<GoodsVO>>();
		if (StringUtils.isEmpty(goodsName) || storeId == null || shopperId == null) {
			res.setStatusInfo(StatusInfo.ERROR_40002);
			res.setSubMsg("参数异常！请检查参数");
			return res;
		}
		res = goodsService.getGoodsInfoByGoodsName(shopperId, storeId, shopId, machineId, goodsName);
		return res;
	}
	
	@RequestMapping(value = "getSingleGoodsPropInfo", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "获取单个商品绑定的菜品属性及加料信息（可调用）")
	public CoreResponse<Map<Integer, GoodsPropsCVO>> getSingleGoodsPropInfo(HttpSession session,
			@RequestParam(required = true) Integer goodsId, @RequestParam(required = true) Integer shopperId,
			@RequestParam(required = true) Integer storeId){

		CoreResponse<Map<Integer, GoodsPropsCVO>> res = new CoreResponse<Map<Integer, GoodsPropsCVO>>();
		if(goodsId==null || shopperId==null || storeId==null){
			res.setStatusInfo(StatusInfo.ERROR_40002);
			res.setSubMsg("请求参数异常！请检查参数");
			return res;
		}
		Map<Integer, GoodsPropsCVO> goodsProps = goodsService.getSingleGoodsPropInfo(goodsId, storeId, shopperId);

		res.setData(goodsProps);
		return res;
	}
	
	@RequestMapping(value = "getSingleNormalGoodsPackage", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "获取单个商品的普通套餐商品详情（可调用）")
	public CoreResponse<List<OrdinaryPackageVO>> getSingleNormalPackageGoods(HttpSession session,
			@RequestParam(required = true) Integer shopperId, @RequestParam(required = true) Integer goodsId,
			@RequestParam(required = true) Integer storeId) {
		
		CoreResponse<List<OrdinaryPackageVO>> res = new CoreResponse<List<OrdinaryPackageVO>>();
		if(shopperId==null || goodsId==null){
			res.setStatusInfo(StatusInfo.ERROR_40002);
			res.setSubMsg("参数异常！请检查参数");
			return res;
		}
		List<OrdinaryPackageVO> NormalPackages = goodsService.getSingleNormalPackageGoods(shopperId,storeId,goodsId);
		res.setData(NormalPackages);
		return res;
	}
	
	@RequestMapping(value = "getSingleGroupPackageGoodsByProp", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "获取单个商品的组合套餐信息（包含加料及做法属性）（可调用）")
	public CoreResponse<List<GroupPackageSuitVO>> getSingleGroupPackageGoodsByProp(HttpSession session,
			@RequestParam(required = true) Integer shopperId, @RequestParam(required = true) Integer storeId,
			@RequestParam(required = true) Integer goodsId) {

		CoreResponse<List<GroupPackageSuitVO>> res = new CoreResponse<List<GroupPackageSuitVO>>();
		if(shopperId==null || goodsId==null || storeId==null){
			res.setStatusInfo(StatusInfo.ERROR_40002);
			res.setSubMsg("参数异常！请检查参数");
			return res;
		}
		List<GroupPackageSuitVO> groupPackages = goodsService.getSingleGroupPackageGoodsByProp(storeId, shopperId,
				goodsId);
		
		res.setData(groupPackages);
		return res;
	}
	
	@RequestMapping(value = "getCopyPageInfo", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "仿菜页面获取桌台及菜品信息(扫码下单调用接口，在线订单没有改业务)")
	public CoreResponse<List<CopyGoodsVO>> getCopyPageInfo(HttpSession session,
			@RequestParam(required=true) Integer storeId,@RequestParam(required=true) String tableUid) {
		CoreResponse<List<CopyGoodsVO>> res = new CoreResponse<List<CopyGoodsVO>>();
		if(storeId==null || StringUtils.isEmpty(tableUid)){
			res.setStatusInfo(StatusInfo.ERROR_40002);
			res.setSubMsg("参数异常！请检查参数");
			return res;
		}
		res = goodsService.getCopyPageInfo(storeId, tableUid);
		return res;
	}
	
	@RequestMapping(value = "getKoubeiRecommend", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "获取购物车页面口碑推荐菜（可调用）")
	public CoreResponse<List<KoubeiRecommendGoodsVO>> getKoubeiRecommend(HttpSession session,
			@RequestBody(required = true) KoubeiRecomEntryVO KoubeiRecomEntry) {
		CoreResponse<List<KoubeiRecommendGoodsVO>> res = new CoreResponse<List<KoubeiRecommendGoodsVO>>();
		
		if (StringUtils.isEmpty(KoubeiRecomEntry.getBuyerId()) || StringUtils.isEmpty(KoubeiRecomEntry.getShopId())
				|| StringUtils.isEmpty(KoubeiRecomEntry.getMerchantPid()) || KoubeiRecomEntry.getStoreId() == null) {
			res.setStatusInfo(StatusInfo.ERROR_40002);
			return res;
		}
		//判断推荐菜类型为ASSOCIATION_RECOMAND关联推荐 dishList不能为空
		if ((Constants.KoubeiDishType.ASSOCIATION_RECOMAND.getValue()).equals(KoubeiRecomEntry.getType())
				&& CollectionUtils.isEmpty(KoubeiRecomEntry.getDishList())) {
			res.setStatusInfo(StatusInfo.ERROR_40002);
			res.setSubMsg("口碑推荐菜品dishList有误");
			return res;
		}
		//判断推荐菜类型为ASSOCIATION_RECOMAND:关联推荐 时，dishList不为空且dish_id不能为空
		if(!CollectionUtils.isEmpty(KoubeiRecomEntry.getDishList())){
			Iterator<KoubeiDishEntryVO> koubeiDish =KoubeiRecomEntry.getDishList().listIterator();
			while(koubeiDish.hasNext()){
				if(StringUtils.isEmpty(koubeiDish.next().getDish_id())){
					res.setStatusInfo(StatusInfo.ERROR_40002);
					res.setSubMsg("口碑推荐菜品dishList有误");
					return res;
				}
			}
		}
		res = goodsService.getKoubeiRecommendGoodsList(KoubeiRecomEntry);
		return res;
	}
	
	@RequestMapping(value = "getNeedGoodsAddCart", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "获取必点菜并加入购物车操作（可异步调用，当该接口获取成功后再调获取购物车信息接口）")
	public CoreResponse<Map<String,Integer>> getNeedGoodsAddCart(HttpSession session,
			@RequestBody(required=true) NeedGoodsAddCart needGoodsAddCart ) {
		
		CoreResponse<Map<String,Integer>> res = new CoreResponse<Map<String,Integer>>();
		
		if (StringUtils.isEmpty(needGoodsAddCart.getBuyerId()) || needGoodsAddCart.getShopperId() == null
				|| needGoodsAddCart.getStoreId() == null) {
			res.setStatusInfo(StatusInfo.ERROR_40001);
			res.setSubMsg("参数有误！请检查参数");
			return res;
		}
		res = goodsService.getNeedGoodsAddCart(needGoodsAddCart);
		return res;
	}
	
	@RequestMapping(value = "getAlreadyGoods", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "获取已点过的菜品信息")
	public CoreResponse<List <AlreadyGoods>> getAlreadyGoods(HttpSession session,
			@RequestParam(required = true) Integer storeId, @RequestParam(required = true) Long thirdUserInfoId) {
		CoreResponse<List<AlreadyGoods>> res = new CoreResponse<List<AlreadyGoods>>();
		if (storeId == null || thirdUserInfoId==null) {
			res.setStatusInfo(StatusInfo.ERROR_40002);
			res.setSubMsg("获取参数异常");
			return res;
		}
		List<AlreadyGoods> goodsList = goodsService.getAlreadyGoods(storeId, thirdUserInfoId);
		res.setData(goodsList);
		return res;
	}
	
	@RequestMapping(value = "getQrcodeGoodsInfo", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "扫描商品二维码获取商品信息")
	public CoreResponse<QrcodeGoodsVO> getQrcodeGoodsInfo(@RequestParam(required = true) Integer storeId,
			@RequestParam(required = true) Integer goodsId, @RequestParam(required = false) Long sizeId) {
		CoreResponse<QrcodeGoodsVO> res = new CoreResponse<QrcodeGoodsVO>();
		if(storeId==null || goodsId ==null){
			res.setStatusInfo(StatusInfo.ERROR_40002);
			res.setSubMsg("参数异常");
			return res;
		}
		QrcodeGoodsVO qrcodeGoods = goodsService.getQrcodeGoodsInfo(storeId,goodsId,sizeId);
		if(qrcodeGoods!=null){
			res.setData(qrcodeGoods);
		}else{
			res.setStatusInfo(StatusInfo.FORBIDDEN);
		}
		return res;
	}
	
}
